//
//  NSDate+Utils.m
//  DoIT
//
//  Created by 葛欣 on 14/11/24.
//  Copyright (c) 2014年 gex. All rights reserved.
//

#import "NSDate+Utils.h"

@implementation NSDate (Utils)

+ (NSDate *) dateLocal{
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localDate =[date  dateByAddingTimeInterval: interval];
    return localDate;
}

- (NSString*)toStringByWeekday{
    NSInteger weekday = [self weekday];
    NSString *str = @"";
    switch (weekday) {
        case 7: str = @"星期日"; break;
        case 1: str = @"星期一"; break;
        case 2: str = @"星期二"; break;
        case 3: str = @"星期三"; break;
        case 4: str = @"星期四"; break;
        case 5: str = @"星期五"; break;
        case 6: str = @"星期六"; break;
        default: break;
    }
    return str;
}
- (NSInteger)weekday{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps =[calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit) fromDate:self];
    NSInteger weekday = ([comps weekday]+6)%7;
    return weekday==0?7:weekday;// 星期几（注意，周日是“1”，周一是“2”。。。。）
}
- (NSDate*)lastDate{
    NSTimeInterval time = [self timeIntervalSince1970];
    time -=86400;
    return [NSDate dateWithTimeIntervalSince1970:time];
}
- (NSDate*)nextDate{
    NSTimeInterval time = [self timeIntervalSince1970];
    time +=86400;
    return [NSDate dateWithTimeIntervalSince1970:time];
}
- (NSString*)toStringByDay{
    return [self toStringByPattern:@"yyyy-MM-dd"];
}
#pragma -私有 格式化
- (NSString *)toStringByPattern:(NSString *)pattern{
    NSCalendar* cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSLocale * locale = [NSLocale systemLocale];
    NSDateFormatter * df = [[NSDateFormatter alloc] init];
    [df setCalendar:cal];
    [df setLocale:locale];
    [df setDateFormat:pattern];
    return [df stringFromDate:self];
}
@end
